// pages/register1/register1.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: 0,
    license_img_fileList: [],
    legal_card_pros_fileList: [],
    legal_card_cons_fileList: [],
    license_img: "", // 营业执照
    legal_card_pros: "", // 身份证正面
    legal_card_cons: "", // 身份证反面
  },
  // 上传
  afterRead(r) {
    console.log('r', r)
    let {
      url
    } = r.detail.file || {}, {
      type
    } = r.currentTarget.dataset || {}, self = this;
    const uploadTask = wx.uploadFile({
      url: wx.$config.uploadFile,
      filePath: url,
      name: 'file',
      formData: {
        'token': wx.getStorageSync("token")
      },
      success(res) {
        if (res.errMsg == "uploadFile:ok") {
          const data = JSON.parse(res.data),
            imgData = {},
            imgUrl = data.data.url;
          console.log("上传返回的图片", imgUrl)
          if (type == 1) {
            imgData.license_img = imgUrl;
            imgData.license_img_fileList = [{
              url: imgUrl
            }];
          }
          if (type == 2) {
            imgData.legal_card_pros = imgUrl;
            imgData.legal_card_pros_fileList = [{
              url: imgUrl
            }];
          }
          if (type == 3) {
            imgData.legal_card_cons = imgUrl;
            imgData.legal_card_cons_fileList = [{
              url: imgUrl
            }];
          }
          self.setData(imgData)
          console.log("提交数据", imgUrl);
          console.log("渲染完后的数据", self.data)

          console.log(self.data.legal_card_pros_fileList, ":::::")
        }
      },
      fail: Error => {
        console.log(Error, '错误')
      }
    })
  },
  // 删除图片
  deleteImg(e) {
    console.log("删除图片", e)
    let {
      path
    } = e.detail.file || {}, {
      type
    } = e.currentTarget.dataset || {}, self = this, imgData = {};
    if (type == 1) {
      imgData.license_img = '';
      imgData.license_img_fileList = [];
    }
    if (type == 2) {
      imgData.license_img = '';
      imgData.legal_card_pros_fileList = [];
    }
    if (type == 3) {
      imgData.license_img = '';
      imgData.legal_card_cons_fileList = [];
    }
    this.setData(imgData)
  },
  // 下一步
  next() {
    let {
      license_img,
      legal_card_pros,
      legal_card_cons
    } = this.data || {};
    if (!license_img){
      wx.$Toast('请上传营业执照证');
      return;
    }
    if (!legal_card_pros) {
      wx.$Toast('请上传身份证正面');
      return;
    }
    if (!legal_card_cons) {
      wx.$Toast('请上传身份证反面');
      return;
    }
    getApp().setRegisterData({
      license_img,
      legal_card_pros,
      legal_card_cons
    })
    wx.navigateTo({
      url: '/pages/register2/register2',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(getApp().getRegisterData(),':::::::getRegisterData')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})